define(function (require, exports, module) {
    exports.ready = function () {
        require.async('$', function () {
            $(function () {
                var store_id = $('#store_id').val();
                var goods_id = $('#goods_id').val();
                var type = $('.actived').data('type');
                var flag = true,page = 1,address_id,remark,option_flag = true;

                getDetail();
                getData();

                /*图文&评论切换*/
                $('.gc-tab-item').click(function(){
                    $('#tab_container').html('');
                    $(this).addClass('actived').siblings().removeClass('actived');
                    type = $(this).data('type');
                    getData();
                })

                /*加入购物车*/
                $(document).on('click','.addcart',function(){
                    cart();
                })

                /*展示弹窗*/
                $('#reserve').click(function(){
                    $('.pop-box').show();
                    $('.pop-cart').addClass('show');
                })

                /*关闭弹窗*/
                $(document).on('click','.close',function(){
                    $('.pop-box').hide();
                    $('.pop-cart').removeClass('show');
                })

                /*滑动加载*/
                $('.container').scroll(function () {
                    // console.log($('.contain').outerHeight());
                    // console.log($('.contain').scrollTop());
                    console.log($('.container').scrollTop() + $('.container').outerHeight()+1);
                    console.log($('.container-child').outerHeight() + 10);
                    if (($('.container').scrollTop() + $('.container').outerHeight() + 1) >= ($('.container-child').outerHeight() + 10)) {
                        if (flag) {
                            getData();
                        }
                    }
                });

                /*判断文字数量*/
                $(document).on('input propertychange','#remark', function () {
                    remark = $(this).val();
                    var num = Number(remark.length), total = Number($('#total').html());
                    if (num <= total) {
                        $('#rest').html(num);
                    } else {
                        $('#rest').html(total);
                        remark = remark.substring(0, total);
                        $(this).val(remark);
                        fm.alert('输入的文字不能超过200个哟', 'background: #fff;color: #2a2a2a;');
                    }
                });

                /*商品数量加减*/
                $(document).on('click', '.change-num-btn', function () {
                    goods_id = $(this).data('goods_id');
                    var status = $(this).data('status');
                    var num = $(this).siblings('.cart-num').html();

                    if (status == 'plus') {
                        num ++;
                    } else {
                        num --;
                        if (num <= 0) {
                            num = 0;
                        }
                    }
                    $('.cart-num-' + goods_id).html(num);
                })

                $(document).on('click', '.address', function () {
                    address_id = $(this).data('id');
                    $(this).addClass('address-check').siblings().removeClass('address-check');
                })

                /*下单*/
                $(document).on('click','#doOrder',function(){
                    option_flag = false;
                    var order_mark = [];
                    var goods_num = $('.cart-num-' + goods_id).html();
                    order_mark.push({goods_id:goods_id,goods_num:goods_num});
                    if(!address_id){
                        fm.alert('请选择取货地址！','background:#fff;color:#2a2a2a');
                    }else{
                        $.ajax({
                            url: '/restaurant/TrustworthyShop/order',
                            type: 'post',
                            data: {store_id: store_id,order_mark:order_mark,remark:remark,address_id:address_id},
                            dataType: 'json',
                            beforeSend: function () {
                                option_flag = false;
                                $('.circle-loadding').show();
                            },
                            success: function (data) {
                                if(data.status == 'S'){
                                    fm.alert(data.message,'background:#fff;color:#2a2a2a');
                                    setTimeout(function(){
                                        location.href = '/restaurant/TrustworthyShop/usercenter/store_id/'+store_id;
                                    },500);
                                }else{
                                    fm.alert(data.message,'background:#fff;color:#2a2a2a');
                                }
                                option_flag = true;
                                $('.circle-loadding').hide();
                            }
                        })
                    }

                })

                /*获取商品详情*/
                function getDetail(){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/goodDetail',
                        type: 'post',
                        data: {store_id: store_id,goods_id:goods_id},
                        dataType: 'json',
                        beforeSend: function () {
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status == 'S'){
                                var html = '',cart_goods = '',address = '';
                                html += '<img src="'+ data.goodDetail.goods_thumb +'"><div class="gc-block gc-flex"><div class="gc-flex-item"><p>'+ data.goodDetail.goods_name +'</p>' +
                                    '<div class="gc-flex gc-flex-lc"><span class="gc-ft-red price">￥'+ data.goodDetail.goods_price +'</span>' +
                                    '<span class="gc-ft-sm gc-ft-gray">已售'+ data.goodDetail.sale_num +'</span>' +
                                    '</div></div><a href="javascript:;" class="iconfont icon-icon gc-ft-deepgray addcart"></a></div>';
                                $('#gooddetail').html(html);

                                $.each(data.areaData,function(i,k){
                                    address += '<div class="gc-block-line address" data-id="'+ k.area_id +'">'+ k.name +'</div>';
                                })

                                cart_goods = '<div class="item list-item gc-flex gc-flex-lc"><div class="item-img"><img src="'+ data.goodDetail.goods_thumb +'"></div> ' +
                                    '<div class="item-msg gc-flex-item"><div class="gc-flex gc-lex-between"><p class="item-name gc-ft-normal">'+ data.goodDetail.goods_name +'</p>' +
                                    '<a href="javascript:;" class="gc-flex-item gc-ft-right"><span class="iconfont icon-delete gc-ft-gray close"></span></a></div>' +
                                    '<p class="gc-ft-red price">￥'+ data.goodDetail.goods_price +'</p><p class="gc-ft-sm gc-ft-gray">已售'+ data.goodDetail.sale_num +'</p></div></div>' +
                                    '<div class="over_show"><p class="gc-ft-deepgray title">提货地点</p><div class="gc-block">'+ address +'<div class="gc-block-line gc-flex gc-flex-lc">' +
                                    '<p class="gc-ft-deepgray">预订数量</p><div class="quantity gc-flex-item gc-ft-right">' +
                                    '<a href="javascript:;" class="iconfont icon-jian gc-ft-deepgray change-num-btn" data-goods_id="'+ data.goodDetail.goods_id +'" data-status="sub"></a>' +
                                    '<span class="gc-ft-deepgray cart-num cart-num-'+ data.goodDetail.goods_id +'">1</span>' +
                                    '<a href="javascript:;" class="iconfont icon-jia gc-ft-deepgray change-num-btn" data-goods_id="'+ data.goodDetail.goods_id +'" data-status="plus"></a></div></div>' +
                                    '<div class="gc-block-line"><textarea id="remark" placeholder="给卖家留言" rows="2"></textarea>' +
                                    '<p class="gc-ft-right gc-ft-deepgray"><span id="rest">0</span>/<span id="total">200</span></p></div></div></div>' +
                                    '<a href="#" class="fixed-btn gc-ft-center" id="doOrder">确定</a>';
                                $('#cart_goods').html(cart_goods);

                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }

                /*获取图文或评论*/
                function getData(){
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/'+type,
                        type: 'post',
                        data: {store_id: store_id,goods_id:goods_id,p:page},
                        dataType: 'json',
                        beforeSend: function () {
                            flag = false;
                            $('.no_more').remove();
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status == 'S'){
                                var html = '',no_more = '';
                                if(type == 'goodContent'){
                                    html = data.goodContent;
                                    flag = false;
                                    $('#tab_container').html(html);
                                }else{
                                    if(data.commentdata.length > 0) {
                                        $.each(data.commentdata, function (i, k) {
                                            var img = '';
                                            $.each(k.comment_picture, function (i, k) {
                                                img += '<div class="img-box"><img src="' + k + '"></div>';
                                            });
                                            html += '<div class="gc-block-line"><div class="user-msg gc-flex gc-flex-center"><div class="user-img">' +
                                                '<img src="' + k.avatar + '"/></div><p class="gc-flex-item">' + k.user_name + '</p></div>' +
                                                '<div class="score"><span class="gc-ft-deepgray">综合评分</span> ' + k.goods_star + '分</div>' +
                                                '<div class="album gc-flex gc-flex-wrap gc-flex-lc">' + img + '</div>' +
                                                '</div>';
                                        });
                                        page ++;
                                        flag = true;

                                        if(data.commentdata.length < 6) {
                                            flag = false;
                                            no_more = '<p class="gc-ft-center gc-ft-gray gc-ft-sm no_more">没有更多评论咯~</p>';
                                            $('#tab_container').after(no_more);
                                        }
                                        $('#tab_container').append(html);
                                    }else{
                                        if(page == 1) {
                                            no_more = '<p class="gc-ft-center gc-ft-gray gc-ft-sm no_more">暂无评论</p>';
                                            $('#tab_container').append(no_more);
                                        }else{
                                            no_more = '<p class="gc-ft-center gc-ft-gray gc-ft-sm no_more">没有更多评论咯~</p>';
                                            $('#tab_container').after(no_more);
                                        }
                                        flag = false;
                                    }
                                }
                            }

                            $('.circle-loadding').hide();
                        }
                    })
                }

                /*加入购物车*/
                function cart() {
                    $.ajax({
                        url: '/restaurant/TrustworthyShop/addCarts',
                        type: 'post',
                        data: {store_id: store_id,goods_id: goods_id},
                        dataType: 'json',
                        beforeSend: function () {
                            $('.circle-loadding').show();
                        },
                        success: function (data) {
                            if(data.status == 'S'){
                                fm.alert(data.message,'background:#fff;color:#2a2a2a');
                            }else{
                                fm.alert(data.message,'background:#fff;color:#2a2a2a');
                            }
                            $('.circle-loadding').hide();
                        }
                    })
                }

            });
        });
    };
});
